package com.hskn.hss.module.fence.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.fence.entity.Fence;
import com.hskn.hss.module.fence.mapper.FenceMapper;
import com.hskn.hss.module.fence.service.IFenceService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Service
public class FenceServiceImpl extends ServiceImpl<FenceMapper, Fence> implements IFenceService {

    @Resource
    private FenceMapper fenceMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<Fence> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Fence::getCreateTime);
        String depId = params.getString("depId");
        if (StringUtils.isNotEmpty(depId)) {
            queryWrapper.lambda().eq(Fence::getDepid, depId);
        }
        String type = params.getString("type");
        if (StringUtils.isNotEmpty(type)) {
            queryWrapper.lambda().eq(Fence::getType, type);
        }
        String name = params.getString("name");
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.lambda().like(Fence::getName, name);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<Fence> fencePage = new Page<>(page, size, true);
        IPage<Fence> iPage = fenceMapper.selectPage(fencePage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("fences", iPage.getRecords());
        return json;
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("fenceId"))) {
            throw new HssException("参数错误");
        }
        Fence fence = JSON.parseObject(params.toJSONString(), Fence.class);
        fenceMapper.updateById(fence);
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Fence fence = JSON.parseObject(params.toJSONString(), Fence.class);
        fence.setCreateUid(uid);
        fenceMapper.insert(fence);
    }

    @Override
    public void del(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("fenceIds"))) {
            throw new HssException("参数错误");
        }
        String idStr = params.getString("fenceIds");
        String[] ids = idStr.split(",");
        fenceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Fence detail(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("fenceId"))) {
            throw new HssException("参数错误");
        }
        Fence fence = fenceMapper.selectById(params.getString("fenceId"));
        return fence;
    }
}
